<?php

namespace App\Services;

use App\Models\Linkage;
use App\Repositories\LinkageRepository;
use Illuminate\Support\Facades\Cache;

class LinkageService
{
    /**
     * @var LinkageRepository
     */
    private $repository;

    /**
     * LinkageService constructor.
     * @param LinkageRepository $repository
     */
    public function __construct(LinkageRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 创建联动
     *
     * @param array $attributes
     * @return $this|\Illuminate\Database\Eloquent\Model
     */
    public function create($attributes)
    {
        $linkage = $this->repository->create($attributes);
        return $linkage;
    }

    /**
     * 批量添加联动
     *
     * @param $attributes
     * @return mixed
     * @author ysh <397088124@qq.com>
     */
    public function insert($attributes)
    {
        $linkage = $this->repository->insert($attributes);
        return $linkage;
    }

    /**
     * 修改联动
     *
     * @param Linkage $linkage
     * @param $attributes
     * @return bool
     */
    public function update(Linkage $linkage, $attributes)
    {
        $result = $this->repository->update($attributes, $linkage);
        return $result;
    }

    /**
     * 删除配置
     *
     * @param Linkage $linkage
     * @return bool|null
     * @throws \Exception
     */
    public function destroy(Linkage $linkage)
    {
        $result = $linkage->delete();
        return $result;
    }

    /**
     * 获取指定联动项
     *
     * @param int $id
     * @return mixed
     */
    public function getById($id)
    {
        return $this->repository->getById($id);
    }

    /**
     * 根据名称获取联动名
     *
     * @param string $name
     * @return mixed
     */
    public function getByName($name)
    {
        return $this->repository->getByName($name);
    }

    /**
     * 获取联动分页列表
     *
     * @param array $condition
     * @param int $per_page
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function paginate($condition = [], $per_page = 20)
    {
        return $this->repository->paginate($condition, $per_page);
    }


    /**
     * 获取所有联动
     *
     * @param array $condition
     * @return mixed
     * @author ysh <397088124@qq.com>
     */
    public function all($condition = [])
    {
        return $this->repository->all($condition);
    }


    /**
     * 缓存联动
     *
     * @return boolean
     */
    public function cache()
    {
        $linkages = $this->getLinkagesToArray();
        Cache::forever('linkages', $linkages);

        $linkage_category = config('setting.linkage_categories');
        if ($linkage_category) {
            foreach ($linkage_category as $key => $value) {
                Cache::forever('linkages_' . $key, $this->getLinkagesToArray(['category' => $value]));
            }
        }
        return true;
    }


    /**
     * 从缓存中获取所有联动数据
     * @param $category
     * @return array
     */
    public function getLinkagesFromCache($category = "")
    {
        if (!$category) {
            return Cache::rememberForever('linkages', function () {
                return $this->getLinkagesToArray();
            });
        }
        $linkage_category = config('setting.linkage_categories');
        if (in_array($category, array_keys($linkage_category))) {
            $category_value = $linkage_category[$category];

            return Cache::rememberForever('linkages_' . $category, function () use ($category_value){
                return $this->getLinkagesToArray(['category' => $category_value]);
            });
        }
    }


    /**
     * 根据分类从缓存获取pid下级值
     * 作为联动接口调用
     *
     * @param $category
     * @param int $pid
     * @return array
     * @author ysh <397088124@qq.com>
     */
    public function getLinkagesOption($category, $pid = 0)
    {
        $options = $this->getLinkagesFromCache($category);
        $return_arr = [];
        if (!empty($options)) {
            foreach ($options as $opt) {
                if ($pid == $opt['pid']) {
                    $return_arr[] = $opt;
                }
            }
        }
        return $return_arr;
    }


    /**
     * 获取联动数组
     *
     * @param $condition
     * 解析过后的配置数组
     *
     * @return array
     */
    public function getLinkagesToArray($condition = [])
    {
        $linkages = $this->repository->allForCache($condition)->keyBy('id')->toArray();
        return $linkages;
    }
}
